package com.kunbo.cn.app.service.impl;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.kunbo.cn.app.dao.SaleCustomerWaybillDao;
import com.kunbo.cn.app.entity.SaleCustomerWaybill;
import com.kunbo.cn.app.service.SaleCustomerWaybillService;
import com.kunbo.cn.dao.core.inventory.InventoryDao;
import com.kunbo.cn.util.date.SDate;
/**
 *
 * @author bjl  
 * @date 2019年3月27日 
 */
@Service
public class SaleCustomerWaybillServiceImpl implements SaleCustomerWaybillService {
	@Autowired
	private SaleCustomerWaybillDao dao;
	@Autowired
    private JdbcTemplate jdbcTemplate;
	@Override
	public void save(SaleCustomerWaybill waybill) {
		waybill.setState("4");
		waybill.setCreate_time(SDate.getSystemDateToString());
		dao.save(waybill);
	
	}
	@Override
	public List<Map<String, Object>> Carrier(String login_name) {
		String sql="SELECT\r\n" + 
				"	c. NAME as customer\r\n" + 
				"FROM\r\n" + 
				"	business_customer_users_manage w,\r\n" + 
				"	business_customer_manage c\r\n" + 
				"WHERE 1 = 1\r\n" + 
				"AND c.id = w.customer";
		sql +=" and w.login_name =  '" + login_name + "'";
		System.out.println("（*************login_name列表**************）"+login_name);
		List<Map<String, Object>> one = jdbcTemplate.queryForList(sql);
		String t = String.valueOf(one.get(0).get("customer").toString());//customerid
		String sql1 = "SELECT \r\n" + 
				"	u.* \r\n" + 
				"FROM \r\n" + 
				"	transport_unit_management u \r\n" + 
				"WHERE   1 = 1 ";
		System.out.println("（*************t**************）"+t);
		if(t != null && !t.isEmpty()){
			sql1 += " and u.add_mechanism = '" + t + "'";
		}
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql1);
		return list;
	}
	/**
	 * 添加运单后修改订单余量
	 */
	@Override
	public int updateOrderMargin(String OrderCode,String train_number) throws SQLException {
		int n=0;
		//String OrderCode //订单编号
		//String train_number//运输量
		if(OrderCode!=null&&!OrderCode.isEmpty()) {
		String sql="";
		sql +="select\r\n" ; 
		sql +="d.contr_margin  \r\n";
		sql +="FROM business_sale_order_manage  d\r\n" ; 
		sql +="WHERE  d.sale_order_code='"+OrderCode+"'\r\n";
		List<Map<String,Object>> list = jdbcTemplate.queryForList(sql);
		Float t = (Float) list.get(0).get("contr_margin");
		Float t1=Float.parseFloat(train_number);
		Float lowerAmount=t-t1;
		
		if(lowerAmount>=t1) {
		String sql3 = "UPDATE \r\n" +
		        "business_sale_order_manage d \r\n"+
		        " set d.contr_margin="+lowerAmount+"\r\n "+
		        " WHERE  d.sale_order_code='"+OrderCode+"'";
				
				 n=jdbcTemplate.update(sql3);
		}else {
			n=0;
		     }
		}else {
			n=0;
		}
		return n;
	}
	@Override
	public List<Map<String, Object>> waybillList(String login_name,Integer page,Integer pageSize) {
		String sql="SELECT\r\n" + 
				"	c. NAME as customer\r\n" + 
				"FROM\r\n" + 
				"	business_customer_users_manage w,\r\n" + 
				"	business_customer_manage c\r\n" + 
				"WHERE 1 = 1\r\n" + 
				"AND c.id = w.customer";
		sql +=" and w.login_name =  '" + login_name + "'";
		System.out.println("（*************login_name列表**************）"+login_name);
		List<Map<String, Object>> one = jdbcTemplate.queryForList(sql);
		String t = String.valueOf(one.get(0).get("customer").toString());//customerid
		String sql1 = "SELECT \r\n" + 
				"	w.* \r\n" + 
				"FROM \r\n" + 
				"	business_waybill_manage w \r\n" + 
				"WHERE   1 = 1 ";
		if(t != null && !t.isEmpty()){
			sql1 += " and w.customer = '" + t + "'";
		}
		sql1 += " order by id  limit " + (page * pageSize) +"," + pageSize;
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql1);
		return list;
	}
	
}
